<template>
  <div class="reconciliation">
    <el-collapse v-model="activeNames" class="custom-collapse">
      <el-collapse-item name="1">
        <template slot="title">
          <div class="title search-title">精准查找</div>
        </template>
        <div class="search">
          <div class="input">
            <label>商户编号：</label>
            <el-input v-model.trim="params.search.merchant_no" clearable></el-input>
          </div>
          <div class="input">
            <label>订单号：</label>
            <el-input v-model.trim="params.search.order_no" clearable></el-input>
          </div>
          <div class="input" style="max-width:480px">
            <label style="min-width: 22%;">对账周期：</label>
            <el-date-picker
              v-model="time"
              style="width:500px!important"
              type="datetimerange"
              range-separator="到"
              start-placeholder="对账开始时间"
              end-placeholder="对账结束时间"
            ></el-date-picker>
          </div>
          <div class="btn">
            <el-button type="primary" @click="search()">查找</el-button>
            <el-button @click="resetParams()">重置</el-button>
          </div>
        </div>
      </el-collapse-item>
    </el-collapse>
    <div class="content">
      <el-table :data="reconciliationList" style="width: 100%" border v-loading="loading">
        <el-table-column width="50" label="序号" header-align="center" align="center">
          <template slot-scope="scope">
            <div>{{params.per_limit*(params.page-1)+scope.$index+1}}</div>
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" prop="id" label="ID" width="60"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="order_no"
          label="订单号"
          width="220"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="merchant_no"
          label="商户编号"
          show-overflow-tooltip
          width="140"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="merchant_name"
          label="商户名称"
          show-overflow-tooltip
          width="130"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="sign_name"
          label="商户签约名"
          show-overflow-tooltip
          width="130"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="total_amount"
          label="订单金额(元)"
          width="95"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="channel_real_credit_amount"
          label="渠道商户入账金额（元）"
          width="160"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="order_created_at"
          label="订单创建时间"
          width="140"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="order_payed_at"
          label="订单支付时间"
          width="140"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="wrong_type_name"
          label="问题类型"
          show-overflow-tooltip
          width="140"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="created_at"
          label="创建时间"
          width="140"
        ></el-table-column>
        <el-table-column
          header-align="center"
          min-width="120"
          align="center"
          fixed="right"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              v-if="searchInterface('order_bill_wrongs.bill_detail')"
              @click="seeDetail(scope.row)"
            >查看上游对账单</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="footer">
        <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="params.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="params.per_limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { orderBillWrongs } from '@/api/reconciliation-and-settlement/reconciliation'
import { date, copy } from '@/utils'
export default {
  data() {
    return {
      loading: false,
      activeNames: ['1'],
      reconciliationList: [],
      time: [],
      params: {
        page: 1,
        per_limit: 10,
        search: {
          order_no: '',
          merchant_no: '',
          create_time_begin: '',
          create_time_end: ''
        }
      },
      total: 0
    }
  },
  beforeMount() {
    this.getData()
  },
  methods: {
    // 查询
    search() {
      this.getData()
    },
    // 重置查找条件
    resetParams() {
      this.time = []
      this.params = {
        page: 1,
        per_limit: 10,
        search: {
          order_no: '',
          merchant_no: '',
          create_time_begin: '',
          create_time_end: ''
        }
      }
      this.getData()
    },
    getData() {
      this.loading = true
      let params = copy.deepCopy(this.params)
      if (this.time && this.time.length) {
        const [begin, end] = this.time
        params.search.create_time_begin = begin.getTime() / 1000
        params.search.create_time_end = end.getTime() / 1000
      }
      orderBillWrongs(params)
        .then(res => {
          const {
            data,
            meta: {
              pagination: { total }
            }
          } = res
          data.forEach(i => {
            i.created_at = date.formatDate(i.created_at)
            i.order_payed_at = date.formatDate(i.order_payed_at)
            i.order_created_at = date.formatDate(i.order_created_at)
          })
          this.reconciliationList = data
          this.total = total
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 切换页码
    currentChange(value) {
      this.params.page = value
      this.getData()
    },
    sizeChange(value) {
      this.params.per_limit = value
      this.getData()
    },
    // 查看详情
    seeDetail({ id }) {
      this.$router.push({
        path: '/menu/reconciliation-and-settlement/reconciliation-detail',
        query: {
          id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.reconciliation {
  width: 100%;
  height: 100%;
}
</style>